12 Exploratory analysis and visualization with ACS data

12.1 Identifying and visualizing the counties from each state with the highest median income

highest_income_counties<-median_income %>% 
                          separate(NAME,c("County","State"),sep=",") %>% 
                          group_by(State) %>% 
                          arrange(desc(median_income)) %>% 
                          slice_max(median_income, n=1) %>% 
                          unite(NAME, c("County","State"), remove=FALSE, sep=",")

highest_income_counties
## # A tibble: 52 x 7
## # Groups:   State [52]
##    GEOID NAME                           County             State            variable  median_income   moe
##    <chr> <chr>                          <chr>              <chr>            <chr>             <dbl> <dbl>
##  1 01117 Shelby County, Alabama         Shelby County      " Alabama"       B19013_0…         77799  2248
##  2 02110 Juneau City and Borough, Alas… Juneau City and B… " Alaska"        B19013_0…         88390  4059
##  3 04013 Maricopa County, Arizona       Maricopa County    " Arizona"       B19013_0…         64468   326
##  4 05007 Benton County, Arkansas        Benton County      " Arkansas"      B19013_0…         66362  1292
##  5 06085 Santa Clara County, California Santa Clara County " California"    B19013_0…        124055  1117
##  6 08035 Douglas County, Colorado       Douglas County     " Colorado"      B19013_0…        119730  1710
##  7 09001 Fairfield County, Connecticut  Fairfield County   " Connecticut"   B19013_0…         95645  1039
##  8 10003 New Castle County, Delaware    New Castle County  " Delaware"      B19013_0…         73892  1210
##  9 11001 District of Columbia, Distric… District of Colum… " District of C… B19013_0…         86420  1008
## 10 12109 St. Johns County, Florida      St. Johns County   " Florida"       B19013_0…         82252  2741
## # … with 42 more rows
highest_income_counties_viz<-
  highest_income_counties %>% 
        ggplot(aes(x=median_income,y=reorder(NAME, median_income)))+
        geom_errorbarh(aes(xmin = median_income - moe, xmax = median_income + moe)) +
        geom_point(color = "red", 
                   size = 3)+
        labs(title="County with Highest Median Income, by State",
             y="",
             x="Median Income Estimate from 5-year ACS\n(bars indicate margin of error)")+ 
        theme(plot.title=element_text(hjust=0.5))
highest_income_counties_viz

12.2 Visualizing county-level median income for Colorado

median_income_CO_counties_2018<-get_acs(geography="county",
                                     state="CO",
                                     variables="B19013_001",
                                     year=2018) %>% 
                            rename(median_income=estimate) %>% 
                            arrange(desc(median_income))
## Getting data from the 2014-2018 5-year ACS
## Using FIPS code '08' for state 'CO'
median_income_CO_counties_2018_viz<-
     median_income_CO_2018 %>% 
        mutate(County_Name=str_remove_all(NAME,"County, Colorado")) %>% 
          ggplot(aes(x=median_income,y=reorder(County_Name, median_income)))+
           geom_errorbarh(aes(xmin = median_income - moe, xmax = median_income + moe)) +
           geom_point(color = "blue", size = 3)+
                      labs(title="Median Income in Colorado, by County (2018)",
                              y="", x="Median Income Estimate from 5 year ACS\n(Bars indicate margin of error)")+
                             theme(plot.title=element_text(hjust=0.5))
median_income_CO_counties_2018_viz  

12.3 Writing functions to visualize ACS data

county_median_income<-function(state_abbrev, state_name){
  median_income<-get_acs(geography="county",
                         state=state_abbrev,
                         variables="B19013_001",
                         year=2018) %>% 
    rename(median_income=estimate) %>% 
    arrange(desc(median_income))
  
  string_to_remove<-paste0("County", ", ", state_name)
  chart_title<-paste0("Median Income in ", state_name, ", by County (2018)")
  
  median_income_viz<-median_income %>% 
                      mutate(County_Name=str_remove_all(NAME,string_to_remove)) %>% 
                      ggplot(aes(x=median_income,y=reorder(County_Name, median_income)))+
                      geom_errorbarh(aes(xmin = median_income - moe, xmax = median_income + moe)) +
                      geom_point(color = "blue", size = 3)+
                      labs(title=chart_title,
                           y="", x="Median Income Estimate from 5 year ACS\n(Bars indicate margin of error)")+
                      theme(plot.title=element_text(hjust=0.5))+
                      scale_x_continuous(labels=scales::comma)
  return(median_income_viz)
}
# test function 
county_median_income("NY", "New York")
## Getting data from the 2014-2018 5-year ACS
## Using FIPS code '36' for state 'NY'

# Creates vector of state abbreviations
state_abbrev_vector<-c("NM", "UT", "AZ", "WY")
# Creates vector of state names
state_name_vector<-c("New Mexico", "Utah", "Arizona", "Wyoming")

# Iteratively applies "county_median_income" function to inputs specified by "state_abbrev" vector
# and "state_name" vector, and assigns resulting visualization outputs to list named "median_income_2018_viz_list"
median_income2018_viz_list<-map2(state_abbrev_vector, state_name_vector, county_median_income)
## Getting data from the 2014-2018 5-year ACS
## Using FIPS code '35' for state 'NM'
## Getting data from the 2014-2018 5-year ACS
## Using FIPS code '49' for state 'UT'
## Getting data from the 2014-2018 5-year ACS
## Using FIPS code '04' for state 'AZ'
## Getting data from the 2014-2018 5-year ACS
## Using FIPS code '56' for state 'WY'
# Creates a vector of names for "median_income2018_viz_list" list elements
names_median_income_list<-paste0(state_abbrev_vector, "_", "2018_ACS_medianincome")
# Inspect names in "names_median_income_list" vector
names_median_income_list
## [1] "NM_2018_ACS_medianincome" "UT_2018_ACS_medianincome" "AZ_2018_ACS_medianincome"
## [4] "WY_2018_ACS_medianincome"
# Assigns names in 
names(median_income2018_viz_list)<-names_median_income_list
median_income2018_viz_list
## $NM_2018_ACS_medianincome

## 
## $UT_2018_ACS_medianincome

## 
## $AZ_2018_ACS_medianincome

## 
## $WY_2018_ACS_medianincome

12.4 Dynamic mapping

median_income_2016_states<-get_acs(geography="state",
                                     variables="B19013_001",
                                     year=2016,
                                     geometry=TRUE) %>% 
                            rename(median_income=estimate) %>% 
                            arrange(desc(median_income)) %>% 
                            relocate(NAME)
## Getting data from the 2012-2016 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
median_income_map<-tm_shape(median_income_2016_states)+
                   tm_polygons(col="median_income", n=6, style="fisher", palette="YlOrBr")
tmap_mode("view")
## tmap mode set to interactive viewing
median_income_map